package com.poisonh.unifiedauth.widget;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.dao.UserinfoDao;
import com.poisonh.unifiedauth.dao.UserinfoDaoImpl;
import com.poisonh.unifiedauth.model.BusinessCard;
import com.poisonh.unifiedauth.model.UserInfoModel;
import com.poisonh.unifiedauth.ui.MainActivity;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.PreferencesUtils;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;

import io.realm.Realm;

public class ResideMenuItem extends LinearLayout
{

    private Context mContext;
    /**
     * menu item  icon
     */
    private CircleImageView iv_icon;
    /**
     * menu item  title
     */
    private TextView tv_title;

    private AutoLinearLayout mAll;

    public ResideMenuItem(Context context)
    {
        super(context);
        this.mContext = context;
        initViews(context);
    }

    public ResideMenuItem(Context context, String title)
    {
        super(context);
        this.mContext = context;
        initViews(context);
        tv_title.setText(title);
        iv_icon.setVisibility(View.GONE);
    }

    public ResideMenuItem(String url, Context context)
    {
        super(context);
        this.mContext = context;
        initViews(context);
        tv_title.setVisibility(View.GONE);
        Glide.with(context).load(url).into(iv_icon);
    }

    public ResideMenuItem(String url, String title, Context context)
    {
        super(context);
        this.mContext = context;
        initViews(context);
        // tv_title.setVisibility(View.GONE);
        mAll.setOrientation(VERTICAL);
        tv_title.setGravity(Gravity.CENTER_HORIZONTAL);
        tv_title.setText(title);
        Glide.with(context).load(url).into(iv_icon);
    }

    public ResideMenuItem(Context context, int icon)
    {
        super(context);
        this.mContext = context;
        initViews(context);
        iv_icon.setImageResource(icon);
    }

    public ResideMenuItem(Context context, int icon, int title)
    {
        super(context);
        this.mContext = context;
        initViews(context);
        iv_icon.setImageResource(icon);
        tv_title.setText(title);
    }

    public ResideMenuItem(Context context, int icon, String title)
    {
        super(context);
        this.mContext = context;
        initViews(context);
        iv_icon.setImageResource(icon);
        tv_title.setText(title);
    }

    private void initViews(Context context)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.residemenu_item, this);
        this.mContext = context;
        iv_icon = (CircleImageView) findViewById(R.id.iv_icon);
        tv_title = (TextView) findViewById(R.id.tv_title);
        mAll = (AutoLinearLayout) findViewById(R.id.all);
    }

    /**
     * set the icon color;
     *
     * @param icon
     */
    public void setIcon(int icon)
    {
        iv_icon.setImageResource(icon);
    }

    /**
     * set the title with resource
     * ;
     *
     * @param title
     */
    public void setTitle(int title)
    {
        tv_title.setText(title);
    }

    /**
     * set the title with string;
     *
     * @param title
     */
    public void setTitle(String title)
    {
        tv_title.setText(title);
    }

    MyReceiver mReceiver = new MyReceiver();

    public class MyReceiver extends BroadcastReceiver
    {
        //可用Intent的getAction()区分接收到的不同广播
        @Override
        public void onReceive(Context context, Intent arg1)
        {
            String action = arg1.getAction();
            if (action.equals("HEAD_CHANGE"))
            {
                changeHeadImg(context);
            }

        }

    }

    /**
     * 注册更改头像地址广播
     */
    public void registerChangeHeadBR()
    {
        IntentFilter filter1 = new IntentFilter();
        filter1.addAction("HEAD_CHANGE");
        mContext.registerReceiver(mReceiver, filter1);
    }

    /**
     * 取消广播
     */
    public void unRegisterChangeHeadBR()
    {
        mContext.unregisterReceiver(mReceiver);
    }

    /**
     * 更改用户头像
     */
    public void changeHeadImg(Context context)
    {
        Context mContext = context.getApplicationContext();
        String mUserId = PreferencesUtils.getString(mContext, "ID");

        try
        {

//            UserinfoDao mUserinfoDaoImpl = new UserinfoDaoImpl(mContext);
//            List<UserInfoModel> model = mUserinfoDaoImpl.queryReturnList(mUserId);

            Realm realm = Realm.getDefaultInstance();
            BusinessCard model = realm.where(BusinessCard.class).equalTo("uid", mUserId).findFirst();
            realm.close();
//            mStrUserHeadUrl = model.get(0).getmStrHeadUrl();
            if (model.getHeadImg()!=null)
            {
                tv_title.setText(model.getName());
            } else
            {
                UserInfoModel userInfoModel =  realm.where(UserInfoModel.class).equalTo("id", mUserId).findFirst();
                tv_title.setText(userInfoModel.getmStrPhone());
            }

            Glide.with(context.getApplicationContext()).load(ConstantUitls.REQUEDT_IMG_URL + model.getHeadImg()).into(iv_icon);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void setImg(String url){
        Glide.with(mContext).load(url).into(iv_icon);
    }
}
